# -*- coding: utf-8 -*-
# Created by 'Zhou Bingbing'  on 2019/4/18
import hashlib
import random
import time
import requests
class YouDao():
    def get_langue(self,text,FROM=None,TO=None):
        url='http://fanyi.youdao.com/bbk/translate_m.do'
        u = 'fanyideskweb'
        d = text
        f = str(int(time.time() * 1000) + random.randint(1, 10))
        c = '@6f#X3=cCuncYssPsuRUE'
        sign = hashlib.md5((u + d + f + c).encode('utf-8')).hexdigest()
        headers={
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate',
            'Accept-Language':'zh-CN,zh;q=0.8',
            'Connection':'keep-alive',
            'Content-Length':'221',
            'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
            'Cookie':'OUTFOX_SEARCH_USER_ID=43732522@10.169.0.84; OUTFOX_SEARCH_USER_ID_NCOO=1140330123.9603255; SESSION_FROM_COOKIE=sogou-inc; JSESSIONID=abca3advC-8Hu_YSRbUOw; _ntes_nnid=3b993b2e5140076960c407074f2ceb2e,1555556922327; YOUDAO_FANYI_SELECTOR=OFF; DICT_UGC=be3af0da19b5c5e6aa4e17bd8d90b28a|; ___rl__test__cookies='+f,
            'Host':'fanyi.youdao.com',
            'Origin':'http://fanyi.youdao.com',
            'Referer':'http://fanyi.youdao.com/',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
            'X-Requested-With':'XMLHttpRequest',
        }
        data={
            'i':d,
            'client':'fanyideskweb',
            'salt':f,
            'sign':sign,
            'from':FROM,
            'to':TO,
            'doctype':'json',
            'version':'3.0',
            'cache':'true',
        }
        re=requests.post(url,data=data,headers=headers,timeout=5)
        results=re.json()
        results=results['translateResult']
        result=[]
        for i in results:
            result.append(i['tgt'].strip('\r\n'))
        return result

    @staticmethod
    def main(text,rest):
        results= YouDao().get_langue(text, FROM='zh-CHS', TO='en')
        for i in results:
            rest.extend(YouDao().get_langue(i, FROM='en', TO='zh-CHS'))
        # print('有道')
        # print(rest)
if __name__=="__main__":
    while 1:
        text=input('请输入中文语料：')
        if not text:
            break
        re=YouDao.main(text)
        print(re)
